package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.domain.SysMomentComment;
import com.ruoyi.project.system.domain.SysMomentFile;
import com.ruoyi.project.system.domain.SysParise;
import com.ruoyi.project.system.service.ISysMomentCommentService;
import com.ruoyi.project.system.service.ISysMomentFileService;
import com.ruoyi.project.system.service.ISysPariseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysMoment;
import com.ruoyi.project.system.service.ISysMomentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 平台精彩瞬间Controller
 *
 * @author jjp
 * @date 2020-04-14
 */
@RestController
@Api(description = "平台精彩瞬间")
@RequestMapping("/system/moment")
public class SysMomentController extends BaseController {
    @Autowired
    private ISysMomentService sysMomentService;

    @Autowired
    private ISysMomentFileService sysMomentFileService;

    @Autowired
    private ISysMomentCommentService sysMomentCommentService;

    @Autowired
    private ISysPariseService sysPariseService;

    /**
     * 查询平台精彩瞬间列表
     */
    @PreAuthorize("@ss.hasPermi('system:moment:list')")
    @ApiOperation("查看所有精彩瞬间")
    @GetMapping("/list")
    public TableDataInfo list(SysMoment sysMoment) {
        startPage();
        List<SysMoment> list = sysMomentService.selectSysMomentList(sysMoment);
        return getDataTable(list);
    }


    /**
     * 获取平台精彩瞬间详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:moment:query')")
    @ApiOperation("查看单个精彩瞬间")
    @GetMapping(value = "/getInfo/{momentId}")
    public AjaxResult getInfo(@PathVariable("momentId") Long momentId) {
        SysMoment sysMoment = sysMomentService.selectSysMomentById(momentId);
        List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < sysMomentFiles.size(); i++) {
            String url = sysMomentFiles.get(i).getUrl();
            list.add(url);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sysMoment", sysMoment);
        map.put("list", list);
        return AjaxResult.success(map);
    }


    /**
     * 获取平台精彩瞬间详细信息
     * <p>
     * APP接口
     */
    @PreAuthorize("@ss.hasPermi('system:moment:query:MomentInfoId')")
    @ApiOperation("查看单个精彩瞬间")
    @GetMapping(value = "/getMomentInfo/{momentId}")
    public AjaxResult getMomentInfo(@PathVariable("momentId") Long momentId) {
        SysMoment sysMoment = sysMomentService.selectSysMomentById(momentId);
        List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
        List<SysMomentComment> sysMomentComments = sysMomentCommentService.selectSysMomentId(momentId);
        List<SysParise> sysParises = sysPariseService.selectSysPariseMomentId(momentId);
        Map<String, Object> map = new HashMap<>();
        map.put("sysMoment", sysMoment);
        map.put("sysMomentFiles", sysMomentFiles);
        map.put("sysMomentComments", sysMomentComments);
        map.put("sysParises", sysParises);
        return AjaxResult.success(map);
    }

    /**
     * 新增平台精彩瞬间
     */
    @PreAuthorize("@ss.hasPermi('system:moment:add')")
    @Log(title = "平台精彩瞬间", businessType = BusinessType.INSERT)
    @ApiOperation("新增平台精彩瞬间")
    @PostMapping
    public AjaxResult add(@RequestBody SysMoment sysMoment) {
        int i = sysMomentService.insertSysMoment(sysMoment);
        List<String> urls = sysMoment.getUrls();
        Long momentId = sysMoment.getMomentId();
        for (int j = 0; j < urls.size(); j++) {
            SysMomentFile sysMomentFile = new SysMomentFile();
            String url = urls.get(j);
            sysMomentFile.setMomentId(momentId);
            sysMomentFile.setUrl(url);
            sysMomentFileService.insertSysMomentFile(sysMomentFile);
        }
        return toAjax(i);
    }

    /**
     * 修改平台精彩瞬间
     */
    @PreAuthorize("@ss.hasPermi('system:moment:edit')")
    @Log(title = "平台精彩瞬间", businessType = BusinessType.UPDATE)
    @ApiOperation("修改平台精彩瞬间")
    @PutMapping
    public AjaxResult edit(@RequestBody SysMoment sysMoment) {
        int i = sysMomentService.updateSysMoment(sysMoment);
        Long momentId = sysMoment.getMomentId();
        sysMomentFileService.deleteSysMomentId(momentId);
        List<String> urls = sysMoment.getUrls();
        for (int j = 0; j < urls.size(); j++) {
            SysMomentFile sysMomentFile = new SysMomentFile();
            String url = urls.get(j);
            sysMomentFile.setMomentId(momentId);
            sysMomentFile.setUrl(url);
            sysMomentFileService.insertSysMomentFile(sysMomentFile);
        }
        return toAjax(i);
    }

    /**
     * 删除平台精彩瞬间
     */
    @PreAuthorize("@ss.hasPermi('system:moment:remove')")
    @Log(title = "平台精彩瞬间", businessType = BusinessType.DELETE)
    @ApiOperation("删除平台精彩瞬间")
    @DeleteMapping("/{momentIds}")
    public AjaxResult remove(@PathVariable Long[] momentIds) {
        return toAjax(sysMomentService.deleteSysMomentByIds(momentIds));
    }

    /**
     * 模糊查询
     */
    @PreAuthorize("@ss.hasPermi('system:moment:likeFind')")
    @ApiOperation("查看所有精彩瞬间")
//    @GetMapping("/likeFind")
    public TableDataInfo likeFind(SysMoment sysMoment) {
        startPage();
        List<SysMoment> list = sysMomentService.selectSysMomentlikeFind(sysMoment);
        return getDataTable(list);
    }

    /**
     * 根据图片视频查询
     */
    @PreAuthorize("@ss.hasPermi('system:moment:likeMomentType')")
    @ApiOperation("根据图片视频查询")
//    @GetMapping("/likeMomentType/{momentType}")
    public TableDataInfo likeMomentType(@PathVariable String momentType) {
        startPage();
        List<SysMoment> list = sysMomentService.selectSysMomentlikeMomentType(momentType);
        return getDataTable(list);
    }

}
